package org.jeecgframework.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.CbsInstallPackageServiceI;
import org.jeecgframework.web.service.ZyFileLogServiceI;
import org.jeecgframework.web.system.pojo.base.CbsInstallPackageEntity;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Title: Controller
 * @Description: 安装包管理
 * @author onlineGenerator
 * @date 2016-10-08 16:11:43
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/cbsInstallPackageController")
public class CbsInstallPackageController extends BaseController {

	@Autowired
	private CbsInstallPackageServiceI cbsInstallPackageService;
	@Autowired
	private ZyFileLogServiceI zyFileLogService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 安装包管理列表 页面跳转
	 */
	@RequestMapping(params = "cbsInstallPackage")
	public ModelAndView cbsInstallPackage(HttpServletRequest request) {
		return new ModelAndView("com/zybk/cbs_install_package/cbsInstallPackageList");
	}

	/**
	 * easyui AJAX请求数据
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(CbsInstallPackageEntity cbsInstallPackage,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(CbsInstallPackageEntity.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, cbsInstallPackage, request.getParameterMap());
		try{
			
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.cbsInstallPackageService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 安装包管理新增页面跳转
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(CbsInstallPackageEntity cbsInstallPackage, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(cbsInstallPackage.getId())) {
			cbsInstallPackage = cbsInstallPackageService.getEntity(CbsInstallPackageEntity.class, cbsInstallPackage.getId());
			req.setAttribute("cbsInstallPackagePage", cbsInstallPackage);
		}
		return new ModelAndView("com/zybk/cbs_install_package/cbsInstallPackage-add");
	}
	

	/**
	 * 添加安装包管理
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(CbsInstallPackageEntity cbsInstallPackage, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "安装包管理添加成功";
		try{
			ZyFileLogEntity fileLog = systemService.get(ZyFileLogEntity.class, cbsInstallPackage.getFileLogId().getId());
			if(null != fileLog){
				cbsInstallPackage.setPackageType(fileLog.getFileType());//方便后续查询，这里保留安装包类型在本表
				String version = cbsInstallPackageService.CalcVersion(fileLog.getFileType());
				cbsInstallPackage.setVersion(version);
				cbsInstallPackageService.save(cbsInstallPackage);
				systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
			}else{
				message = "安装包管理添加失败";
				j.setSuccess(false);
			}
		}catch(Exception e){
			e.printStackTrace();
			j.setSuccess(false);
			message = "安装包管理添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * @author xiul
	 * 2016-10-9 上午10:21:31
	 * Description 扫码 - 安装包下载页 
	 */
	@RequestMapping(params = "goDownloadPackage")
	public ModelAndView goDownloadPackage(HttpServletRequest req) {
		System.out.println("跳转APP下载页...");
		return new ModelAndView("com/zybk/cbs_install_package/cbsInstallPackage-download");
	}
	
	/**
	 * @author xiul
	 * 2016-10-9 上午10:21:09
	 * Description 扫码 - 安装包下载 
	 */
	@ResponseBody
	@RequestMapping(params="downloadPackage")
	public void downloadPackage(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="fileType",defaultValue="")String fileType){
		System.out.println("---------------->>"+fileType);
		String fileLogId = "";
		if(StringUtil.isNotEmpty(fileType)){
			fileLogId = cbsInstallPackageService.getAppFileLogId(fileType,null);
		}
		ZyFileLogEntity zyFileLogEntity = systemService.get(ZyFileLogEntity.class, fileLogId);//这里先用 plist 的filelog id下载。应该是先下载plist ，plist中配置下载地址
		if(zyFileLogEntity != null){
			try {
				UploadFileUtil.downFile(request, response, new FileBean(zyFileLogEntity.getFilePath(),zyFileLogEntity.getOldFileName()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 删除安装包管理
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(CbsInstallPackageEntity cbsInstallPackage, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		cbsInstallPackage = systemService.getEntity(CbsInstallPackageEntity.class, cbsInstallPackage.getId());
		message = "安装包管理删除成功";
		try{
			zyFileLogService.deleteFileLogAndFile(cbsInstallPackage.getFileLogId().getId());
			cbsInstallPackageService.delete(cbsInstallPackage);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "安装包管理删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-10-8 下午6:05:28
	 * Description 上传安装包 
	 */
	@ResponseBody
	@RequestMapping(params = "importPackage")
	public AjaxJson importPackage(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
			MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
			MultipartFile multipartFile = req.getFile("Filedata");
			String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));     
			String filePath = StaticValue.UPLOAD_PATH;
			File fileDirectory = new File(filePath);
			if  (!fileDirectory.exists()  && !fileDirectory.isDirectory()){
				fileDirectory.mkdirs();
			}
			String newFileName = UUID.randomUUID().toString().replace("-", "")+ suffix;
			String fileRealName = multipartFile.getOriginalFilename();
			String uploadPath = filePath + newFileName;
			File file = new File(uploadPath);
			file.createNewFile();
			multipartFile.transferTo(file);
			ZyFileLogEntity zyfileLog = new ZyFileLogEntity();
			zyfileLog.setFilePath(file.getAbsolutePath());
			zyfileLog.setOldFileName(fileRealName);
			zyfileLog.setNewFileName(newFileName);
			zyfileLog.setFileType(suffix.replace(".","").toLowerCase());
			
			systemService.save(zyfileLog);
			j.setObj(zyfileLog);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("上传失败");
		}
		return j;
	}
}
